package com.cims.common.utility;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.ServletOutputStream;


public class FileUtils {
	public FileUtils() {
	}

	public static void outputFile(String strFileName, String strText) throws IOException {
		if(strText == null || strText.equals(""))
			return;

		if(!fileExists(strFileName)) {
			File file = new File(strFileName);
			file.createNewFile();
		}
		
		FileOutputStream fileOutputStream = new FileOutputStream(strFileName, true);
		OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);
		BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
		bufferedWriter.write(strText);
		bufferedWriter.newLine();
		
		bufferedWriter.close();
		outputStreamWriter.close();
		fileOutputStream.close();
	}

	public static boolean fileExists(String strFileName) {
		String filePath = strFileName;
		File file = new File(filePath);
		return file.exists();
	}
	
	synchronized public static String generateFileName() {
		Date dCurrent = new Date();
		String strName = "";
		Calendar cal = Calendar.getInstance();
		cal.setTime(dCurrent);
		strName = strName + cal.get(Calendar.YEAR) + cal.get(Calendar.MONTH) + cal.get(Calendar.DAY_OF_MONTH) + cal.get(Calendar.HOUR) + cal.get(Calendar.MINUTE) +  cal.get(Calendar.SECOND) + cal.get(Calendar.MILLISECOND);

		return strName;
	}
	
	public static java.sql.Date  getDate(String strDate) {
		
		try {
			return java.sql.Date.valueOf(strDate.replaceAll("/", "-"));			
		} catch(Exception e) {
			e.printStackTrace();
			return null;
		}
		
	}
	
	public static boolean deleteDir(File dir) {
		
		if (dir.isDirectory()) {
			
			String[] children = dir.list();
			
			for (int i=0; i<children.length; i++) {
				
				boolean success = deleteDir(new File(dir, children[i]));
			
				if (!success) {
					return false;
				}
			}
		}

		// The directory is now empty so delete it
		return dir.delete();
	}
	
	public static int runCommand(String msg, String cmd, String finishFile, String stopFile) {
		System.gc();
		int exitCode = -1;
		
		try {
			Runtime runtime = Runtime.getRuntime();
			
			if(fileExists(stopFile)) {
				removeFile(stopFile);
				return 0;
			}
			
			exitCode = -2;
			System.out.println(cmd);
			Process process = runtime.exec(cmd);
			
			try {
				
				long intCheck = 0L;
				
				do {
					intCheck++;
					Thread.sleep(500L);
					if(fileExists(finishFile)) {
						process.waitFor();
						break;
					}
					
					if(!fileExists(stopFile))
						continue;
					
					removeFile(stopFile);
					
					break;
					
				} while(600L > intCheck);
				
				exitCode = process.exitValue();
			
			} catch(Exception e) {
				e.printStackTrace();
			}
			
			process.getInputStream();
			process.getOutputStream();
			process.getErrorStream();
			process.destroy();
			
		} catch(Exception e) {
			e.printStackTrace();
		}
		return exitCode;
	}
	
	public static void removeFile(String strFilePathName) {
		File file = new File(strFilePathName);
		file.delete();
	}
	
	public static void makeFile(String strFilePath, String strFileName) throws IOException {
		removeFile(strFilePath + "\\" + strFileName);
		createDir(strFilePath);
		
		try {
			if(!fileExists(strFilePath + "\\" + strFileName)) {
				File file = new File(strFilePath + "\\" + strFileName);
				file.createNewFile();
				System.out.println("Create File : " + strFilePath + "\\" + strFileName);
			}
		}
		catch(IOException e) {
			
		}
	}	
	
	public static void createDir(String strPathDir) {
		if (!fileExists(strPathDir)) {
			File file = new File(strPathDir);
			System.out.println("Create directory : " + " " + strPathDir);
			file.mkdir();
			file.mkdirs();
		}
	}
	
	public static void removeDir(String strDirPath) {
		if(fileExists(strDirPath)) {
		
			File objFiles[] = (new File(strDirPath)).listFiles();
			
			if(objFiles != null) {
				for(int i = 0; i < objFiles.length; i++)
					if(objFiles[i].isDirectory())
						removeDir(strDirPath + "\\" + objFiles[i].getName());
					else
						objFiles[i].delete();

			}
			
			removeFile(strDirPath);
		}
	} 
	
	public static void downloadFileStream(String strFileName, ServletOutputStream out) {
		try {
			
			File inFile = new File(strFileName);
			InputStream in = new FileInputStream(inFile);
			int data = -1;
			long longCount = 0L;
		
			while((data = in.read()) != -1)  {
				out.write(data);
				longCount++;
				if(longCount % 200L == 0L)
					out.flush();
			}
			
			in.close();
			out.close();
			
		} catch(IOException e) { 
			e.printStackTrace();
		}
	}
	
	public static void createFile(String strFilePathName) {
		
		try {
			if(!fileExists(strFilePathName)) {
				File file = new File(strFilePathName);
				file.createNewFile();
			}
		} catch(IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void createFile(String strFilePathName, String strFileName) {
		
		try {
			if(!fileExists(strFilePathName + "\\" + strFileName) && fileExists(strFilePathName)) {
				File file = new File(strFilePathName + "\\" + strFileName);
				file.createNewFile();
			}
		} catch(IOException e) {
			e.printStackTrace();
		}
	}  
	
	public static void copyFile(String strPathFileNameFrom, String strPathFileNameTo) throws FileNotFoundException, IOException {
		
		FileInputStream fIn = new FileInputStream(strPathFileNameFrom);
		FileOutputStream fOut = new FileOutputStream(strPathFileNameTo);
		int b;
		
		try {
			while((b = fIn.read()) != -1) 
				fOut.write((byte)b);
		} catch(FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			fIn.close();
			fOut.close();
		}
	}

	public static boolean isFile(String strFileName) {
		File file = new File(StringUtils.trimString(strFileName));
		
		if (file.exists() == true) {
			return file.isFile();
		} else {
			return false;
		}
	}
}

